var app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        myself: false,
        disabled: true,
        user: {},
        colorIndex: 0,
        colorData: [],
		buttom:"-60vh"
    }, 
  	comfirm(){
		let status = this.selectComponent("#invite").invite()
		console.log(status)
		if(status == 0){
			wx.showToast({
				title: '已经邀请过了',
				icon:"none"
			})
			return
		} 
		wx.showToast({
			title: '邀请成功',
			icon: "none"
		})
		this.cancel()
	},
	toInvite(){
		this.setData({
			buttom:"0"
		})
	},
	cancel(){
		this.setData({
			buttom: "-60vh"
		})
	},
    colorChange(e) {
        // console.log(e)
        let color = this.data.colorData[e.detail.value]
        let cardcolor = "user.cardcolor"
        this.setData({
            colorIndex: e.detail.value,
            cardcolor: color,
            [cardcolor]: color
        })
    },
    updateUser(e) {
        wx.showLoading({
            title: '保存中...',
        })
        // console.log(e.detail.value)
        let userData = e.detail.value
        userData['isComplete'] = true
        userData.cardcolor = this.data.cardcolor
        console.log(userData)
		for (let key in userData) {			
			if (typeof (userData[key]) == "string" && userData[key].replace(/\s+/g, '') == '') {//信息不能为空
				wx.showToast({
					title: '信息都不能为空',
					icon: 'none',
					duration: 1500
				})
				return
			}
		}
        db.collection("users").doc(this.data.user._id).update({
            data: userData
        }).then(res => {
            console.log("保存成功")
            wx.hideLoading()
            wx.showToast({  
                title: '操作成功！', // 标题
                  icon: 'success',
                  // 图标类型，默认success
                  duration: 500  // 提示窗停留时间，默认1500ms
            })
            setTimeout(function() {
                wx.navigateBack({
                    data: 1
                })
            }, 500)
        })
    },
    getIndexCardcolor(item, arr) {
        let index = 0
        for (let i = 0; i < arr.length; i++) {
            if (item == arr[i]) {
                index = i
            }
        }
        return index
    },
	addScore(){
		wx.cloud.callFunction({
			name: "updateScore",
			data:{
				id:this.data.user._id
			}
		}).then(res => {
			console.log(res)
			wx.showToast({
				title: '已赞', // 标题
				icon: 'success',
				// 图标类型，默认success
				duration: 500  // 提示窗停留时间，默认1500ms
			})
			let scoredata = "user.score"
			this.setData({
				[scoredata]: this.data.user.score+1
			})
		})
	},
	inputChange(e){
		console.log(e)
		let change = e.currentTarget.id
		console.log(change)
		let changeInput = "user."+change
		console.log(changeInput)
		this.setData({
			[changeInput]:e.detail.value
		})
	},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		this.setData({
			openid:options.openid
		})
        //判断是否本人打开
        // console.log(options)
        if (options.openid == app.globalData.openid) {
            this.setData({
                //input可以编辑
                disabled: false,
                myself: true
            })
        } else {
            myself: false
        }
        //信息
        db.collection("users").where({
            _openid: options.openid
        }).get().then(res => {
            console.log(res)
            db.collection("cardcolor").get().then(e => {
                //更新用户卡片颜色在picker中
                let index = this.getIndexCardcolor(res.data[0].cardcolor, e.data[0].color)
                //初始化用户信息和获取卡片picker
                this.setData({
                    colorIndex: index,
                    colorData: e.data[0].color,
                    user: res.data[0]
                })
            })
        })
    },
})